package com.sczmgk.hotline.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.sczmgk.hotline.R;


/**
 * test 公共的dialog样式
 * @description
 */
public abstract class BaseDialog extends Dialog implements View.OnClickListener {
	private final String TAG = "BaseDialog";
    public TextView tvTitle;

    private FrameLayout mContent;

    private TextView btnTopLine;
    private TextView btn1RightLine;
    private TextView btn2RightLine;
    private TextView btn3RightLine;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private LinearLayout btnContent;

    private View.OnClickListener mBtn1ClickListener;
    private View.OnClickListener mBtn2ClickListener;
    private View.OnClickListener mBtn3ClickListener;
    private View.OnClickListener mBtn4ClickListener;

    private boolean isAutoDismiss1 = true;

    private boolean isAutoDismiss2 = true;

    private boolean isAutoDismiss3 = true;

    private boolean isAutoDismiss4 = true;

    public BaseDialog(Context context) {
        super(context);
        init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(createContentView());
    }
	/**
	 * 初始化Dialog
	 */
    private void init() {

        this.getContext().setTheme(android.R.style.Theme_InputMethod);
        super.setContentView(R.layout.common_base_dialog);
        btnContent = (LinearLayout) findViewById(R.id.ll_btn_content);

        tvTitle = (TextView) findViewById(R.id.tv_title);
        mContent = (FrameLayout) findViewById(R.id.fl_content);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btnTopLine = (TextView)findViewById(R.id.tv_btn_top_line);
        btn1RightLine = (TextView) findViewById(R.id.tv_btn1_right_line);
        btn2RightLine = (TextView) findViewById(R.id.tv_btn2_right_line);
        btn3RightLine = (TextView) findViewById(R.id.tv_btn3_right_line);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

        Window window = getWindow();
        WindowManager.LayoutParams attributesParams = window.getAttributes();
        attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        attributesParams.dimAmount = 0.4f;
        int width = (int) (window.getWindowManager().getDefaultDisplay().getWidth() * 0.8f);//设置dialog宽度
        window.setLayout(width, LayoutParams.WRAP_CONTENT);
    }

    /**
     * 设置按钮上的线是否显示
     * @param visibility
     */
    public void setBtnTopLineVisibility(int visibility){
    	btnTopLine.setVisibility(visibility);
    }
    /**
     * 设置dialog的标题
     *
     * @param title
     */
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    /**
     * 设置title　　gravity
     * @param gravity
     */
    public void setTitleGravity(int gravity){
    	tvTitle.setGravity(gravity);
    }
    /**
     * 设置title字体颜色
     * @param ResId
     */
    public void setTitleTextColor(int ResId){
    	tvTitle.setTextColor(ResId);
    }
    /**
     * 设置标题文字的显示与隐藏
     *
     * @param bool
     */
    public void setTitleEnable(boolean bool) {
        if (bool) {
            tvTitle.setVisibility(View.VISIBLE);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
    }

    /**
     * 设置dialog的标题
     *
     * @param resId
     * @see Dialog#setTitle(int)
     */
    public void setTitle(int resId) {
        setTitle(getContext().getResources().getString(resId));
    }

    public void goneHeadTitle() {
        findViewById(R.id.linearlayout_head_title).setVisibility(View.GONE);
    }

    /**
     * 构建dialog的内容视图
     * 
     * @return
     */
    public abstract View createContentView();

	public void setContentView(View view) {
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        mContent.addView(view);
    }

    /**
     * 设置按钮1的文字
     * 
     * @param text
     */
    public void setBtn1Text(String text) {
        btn1.setText(text);
    }

    /**
     * 设置按钮2的文字
     * 
     * @param text
     */
    public void setBtn2Text(String text) {
        btn2.setText(text);
    }

    /**
     * 设置按钮3的文字
     * 
     * @param text
     */
    public void setBtn3Text(String text) {
        btn3.setText(text);
    }

    /**
     * 设置按钮4的文字
     * 
     * @param text
     */
    public void setBtn4Text(String text) {
        btn4.setText(text);
    }

    /**
     * 设置按钮1的文字
     * 
     * @param resId
     */
    public void setBtn1Text(int resId) {
        btn1.setText(resId);
    }

    /**
     * 设置按钮2的文字
     * 
     * @param resId
     */
    public void setBtn2Text(int resId) {
        btn2.setText(resId);
    }

    /**
     * 设置按钮3的文字
     * 
     * @param resId
     */
    public void setBtn3Text(int resId) {
        btn3.setText(resId);
    }

    /**
     * 设置按钮4的文字
     * 
     * @param resId
     */
    public void setBtn4Text(int resId) {
        btn4.setText(resId);
    }

    /**
     * 设置点击按钮1后时候是否关闭dialog 默认为true
     * 
     * @param autoDismiss
     */
    public void setAutoDismiss1(boolean autoDismiss) {
        isAutoDismiss1 = autoDismiss;
    }

    /**
     * 设置点击按钮2后时候是否关闭dialog 默认为true
     * 
     * @param autoDismiss
     */
    public void setAutoDismiss2(boolean autoDismiss) {
        isAutoDismiss2 = autoDismiss;
    }

    /**
     * 设置点击按钮3后时候是否关闭dialog 默认为true
     * 
     * @param autoDismiss
     */
    public void setAutoDismiss3(boolean autoDismiss) {
        isAutoDismiss3 = autoDismiss;
    }

    /**
     * 设置点击按钮4后时候是否关闭dialog 默认为true
     * 
     * @param autoDismiss
     */
    public void setAutoDismiss4(boolean autoDismiss) {
        isAutoDismiss4 = autoDismiss;
    }

    /**
     * 设置按钮1是否可见
     * 
     * @param visible
     */
    public void setBtn1Visible(boolean visible) {
        if (visible) {
            btn1.setVisibility(View.VISIBLE);
            btn1RightLine.setVisibility(View.VISIBLE);
        } else {
            btn1.setVisibility(View.GONE);
            
            btn1RightLine.setVisibility(View.GONE);
        }
    }

    /**
     * 设置按钮2是否可见
     * 
     * @param visible
     */
    public void setBtn2Visible(boolean visible) {
        if (visible) {
        	btn2RightLine.setVisibility(View.GONE);
            btn2.setVisibility(View.VISIBLE);
        } else {
            btn2.setVisibility(View.GONE);
            btn2RightLine.setVisibility(View.GONE);
        }
    }

    /**
     * 设置按钮3是否可见
     * 
     * @param visible
     */
    public void setBtn3Visible(boolean visible) {
        if (visible) {
        	btn3RightLine.setVisibility(View.VISIBLE);
            btn3.setVisibility(View.VISIBLE);
        } else {
            btn3.setVisibility(View.GONE);
            btn3RightLine.setVisibility(View.GONE);
        }
    }

    /**
     * 设置按钮4是否可见
     * 
     * @param visible
     */
    public void setBtn4Visible(boolean visible) {
        if (visible) {
            btn4.setVisibility(View.VISIBLE);
        } else {
            btn4.setVisibility(View.GONE);
        }
    }

    /**
     * 设置按钮1是否处于可点击状态
     * 
     * @param enabled
     */
    public void setBtn1Enable(boolean enabled) {
        btn1.setEnabled(enabled);
    }

    /**
     * 设置按钮2是否处于可点击状态
     * 
     * @param enabled
     */
    public void setBtn2Enable(boolean enabled) {
        btn2.setEnabled(enabled);
    }

    /**
     * 设置按钮3是否处于可点击状态
     * 
     * @param enabled
     */
    public void setBtn3Enable(boolean enabled) {
        btn3.setEnabled(enabled);
    }

    /**
     * 设置按钮4是否处于可点击状态
     * 
     * @param enabled
     */
    public void setBtn4Enable(boolean enabled) {
        btn4.setEnabled(enabled);
    }

    /**
     * @param listener
     */
    public void setBtn1ClickListener(View.OnClickListener listener) {
        mBtn1ClickListener = listener;
    }

    /**
     * @param listener
     */
    public void setBtn2ClickListener(View.OnClickListener listener) {
        mBtn2ClickListener = listener;
    }

    /**
     * @param listener
     */
    public void setBtn3ClickListener(View.OnClickListener listener) {
        mBtn3ClickListener = listener;
    }

    /**
     * @param listener
     */
    public void setBtn4ClickListener(View.OnClickListener listener) {
        mBtn4ClickListener = listener;
    }

    /**
     * @param color
     */
    public void setBtn1TextColor(int color) {
        btn1.setTextColor(color);
    }

    public void setBtn1Background(int resid) {
        btn1.setBackgroundResource(resid);
    }

    /**
     * @param color
     */
    public void setBtn2TextColor(int color) {
        btn2.setTextColor(color);
    }

    /**
     * @param resid
     */
    public void setBtn2Background(int resid) {
        btn2.setBackgroundResource(resid);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn1:
                if (mBtn1ClickListener != null) {
                    mBtn1ClickListener.onClick(v);
                }
                if (isAutoDismiss1) {
                    dismiss();
                }
                break;
            case R.id.btn2:
                if (mBtn2ClickListener != null) {
                    mBtn2ClickListener.onClick(v);
                }
                if (isAutoDismiss2) {
                    dismiss();
                }
                break;
            case R.id.btn3:
                if (mBtn3ClickListener != null) {
                    mBtn3ClickListener.onClick(v);
                }
                if (isAutoDismiss3) {
                    dismiss();
                }
                break;
            case R.id.btn4:
                if (mBtn4ClickListener != null) {
                    mBtn4ClickListener.onClick(v);
                }
                if (isAutoDismiss4) {
                    dismiss();
                }
                break;
            default:
                break;
        }
    }
    /**
     * 不显示下面的按钮
     */
    protected void setBtnConetntGone(){
    	btnContent.setVisibility(View.GONE);
    }

}
